﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Linq;
using LinqObj;
/// <summary>
/// Summary description for Category
/// </summary>

namespace LinqObj
{
    public partial class Service_Category
    {
        public List<Service> MyServices = new List<Service>();
        private static ToolboxDataContext _context = new ToolboxDataContext();

        public static IDictionary<int, Service_Category> LoadServiceCategories(string agentGroupID)
        {
            return LoadServiceCategories(Convert.ToInt32(agentGroupID));
        }

        public static IDictionary<int, Service_Category> LoadServiceCategories(int agentGroupID)
        {
            var set = from _index in _context.GetTable<Service_AgentGroup_Index>()
                      where _index.agent_group_id == agentGroupID
                      select _index.service_id;

            IEnumerable<Service> services = from _service in _context.GetTable<Service>()
                                            where set.Contains(_service.service_id)
                                            select _service;

            IDictionary<int, Service_Category> lookups = (from _category in _context.GetTable<Service_Category>()
                                                          where _category.Services.ToList().Intersect(services).Count() > 0
                                                          orderby _category.order_number
                                                          select _category).ToDictionary(p => p.service_category_id);            

            foreach (int _key in lookups.Keys)
            {
                lookups[_key].MyServices.Clear();
            }

            foreach (Service _service in services)
            {
                if (!lookups[_service.service_category_id].MyServices.Contains(_service))
                    lookups[_service.service_category_id].MyServices.Add(_service);
            }
            return lookups;
            //IDictionary<int, Category> categoryLookups = Category.LoadServiceCategoryLookups(conn, agentGroupID);
            //IEnumerable<Service> serviceList = Service.LoadServiceList(conn, agentGroupID);
            //IEnumerator<Service> sEnumerator = serviceList.GetEnumerator();
            //while (sEnumerator.MoveNext())
            //{
            //    Service service = (Service)sEnumerator.Current;
            //    Category category = categoryLookups[service.CategoryID];
            //    category.serviceList.Add(service);
            //}
            //return categoryLookups;
        }
    }
}
//    private int categoryID;
//    private string categoryName;
//    private string icon;
//    private List<Service> serviceList;

//    public int CategoryID { get { return this.categoryID; } set { this.categoryID = value; } }
//    public string CategoryName { get { return this.categoryName; } set { this.categoryName = value; } }
//    public string Icon { get { return this.icon; } set { this.icon = value; } }
//    public List<Service> ServiceList { get { return this.serviceList; } set { this.serviceList = value; } }

//    public Category()
//    {
//        serviceList = new List<Service>();
//    }

//    public static IDictionary<int, Category> LoadServiceCategories(IDbConnection conn, string agentGroupID)
//    {
//        return LoadServiceCategories(conn, Convert.ToInt32(agentGroupID));
//    }


//    public static IDictionary<int, Category> LoadServiceCategories(IDbConnection conn, int agentGroupID)
//    {
//        IDictionary<int, Category> categoryLookups = Category.LoadServiceCategoryLookups(conn, agentGroupID);
//        IEnumerable<Service> serviceList = Service.LoadServiceList(conn, agentGroupID);
//        IEnumerator<Service> sEnumerator = serviceList.GetEnumerator();
//        while (sEnumerator.MoveNext())
//        {
//            Service service = (Service)sEnumerator.Current;
//            Category category = categoryLookups[service.CategoryID];
//            category.serviceList.Add(service);
//        }
//        return categoryLookups;
//    }

//    public static IDictionary<int, Category> LoadServiceCategoryLookups(IDbConnection conn, int agentGroupID)
//    {
//        IDictionary<int, Category> categoryLookups = new Dictionary<int, Category>();
//        string sql_script =
//                              String.Format(@"select * from service_category where service_category_id in(
//                                    select service_category_id from service
//                                where service_id in(
//                                    select service_id 
//                                from service_agentgroup_index
//                                where agent_group_id = {0}))", agentGroupID.ToString());

//        IDbCommand cmd = conn.CreateCommand();
//        String.Format(sql_script, agentGroupID.ToString());
//        cmd.CommandText = sql_script;
//        IDataReader dr = cmd.ExecuteReader();

//        while (dr.Read())
//        {
//            Category category = new Category();
//            category.categoryID = Convert.ToInt32(dr["service_category_id"].ToString());
//            category.categoryName = dr["service_category"].ToString();
//            category.icon = dr["icon"].ToString();
//            categoryLookups.Add(category.CategoryID, category);
//        }
//        return categoryLookups;
//    }